//
//  main.m
//  OC05_Homework
//
//  Created by dllo on 16/7/8.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Person.h"
#import "ProModel.h"
#import "CityModel.h"
#import "AreaModel.h"
#import "TempCity.h"
int main(int argc, const char * argv[]) {
#pragma mark - 第一题
#if 0
    {
    /*
     1.创建1个包含5个字符串元素的数组；
     2.用for循环倒序输出所有元素；
     3.用for...in完成所有字符串元素的拼接(生成一个新的字符串)，并输出拼接好的字符串。
     */
    NSArray *stringArr = @[@"韩士兵", @"黄书包", @"白书包", @"小书包", @"水杯"];
    for (NSInteger i = (stringArr.count - 1); i >=0; i--) {
        NSLog(@"string = %@", stringArr[i]);
    }
    NSString *newString = [NSString string];
    for (NSString *str in stringArr) {
        newString = [newString stringByAppendingString:str];
    }
    NSLog(@"newString = %@", newString);
    
#pragma mark - 第二题
    /*
     1.创建1个包含5个person对象的数组(姓名用英文)；
     2.用for循环找到年龄最大的Person对象；
     3.用for...in遍历数组，把大于某个年龄的Person对象的姓名后面拼接上字符串“-brother”。
     */
    Person *person1 = [Person personWithName:@"Mike" sex:@"男" age:18];
    Person *person2 = [Person personWithName:@"Jack" sex:@"男" age:20];
    Person *person3 = [Person personWithName:@"Jane" sex:@"女" age:18];
    Person *person4 = [Person personWithName:@"Tom" sex:@"男" age:25];
    Person *person5 = [Person personWithName:@"Nancy" sex:@"女" age:30];
    NSArray *personArr = @[person1, person2, person3, person4, person5];
    
    NSInteger max = 0;
    for (int i = 0; i < personArr.count; i++) {
        max = max > [personArr[i] age] ? max : [personArr[i] age];
    }
    for (int i = 0; i < personArr.count; i++) {
        if ([personArr[i] age] == max) {
            NSLog(@"年龄最大的%@", personArr[i]);
        }
    }
    
    NSInteger number = (arc4random() % (10)) + 20;
    for (Person *person in personArr) {
        if (person.age > number) {
            person.name = [person.name stringByAppendingString:@"-brother"];
            NSLog(@"年龄大于%ld的%@", number, person);
        }
    }
#pragma mark - 第三题
    /* 三、
     1.使用第二题目中生成的数组；
     2.使用sortUsingSelector: 分别按年龄，姓名，性别进行排序，输出排序结果；
     3.使用NSSortDescriptor，按姓名进行排序。
     */
    // 2
    personArr = [personArr sortedArrayUsingSelector:@selector(sortByAge:)];
    NSLog(@"%@", personArr);
    personArr = [personArr sortedArrayUsingSelector:@selector(sortByName:)];
    NSLog(@"%@", personArr);
    personArr = [personArr sortedArrayUsingSelector:@selector(sortBySex:)];
    NSLog(@"%@", personArr);
    // 3
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
    personArr = [personArr sortedArrayUsingDescriptors:@[sortDescriptor]];
    // 按姓名排序NSSortDescriptor
    NSLog(@"%@", personArr);
    }
#endif
#pragma mark - 第四题
#if 1
    {
    /* 四、
     处理省市区文件中的area.txt将省市区进行分割处理, 省里包含市, 市里包含区
     1. 先用数组字典表示
     2. 将数组字典的处理方式转换成数组模型(Class)
     */
    NSString *path = @"/Users/dllo/DLS160710/Objective-C/HomeWork/OC05_Homework/OC05_Homework/area.txt";
    NSString *fileStr = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    // 通过\n分割为数组
    NSArray *arr = [fileStr componentsSeparatedByString:@"\n"];
    // 省数组 存储每个省字典
    NSMutableArray *provincesArr = [NSMutableArray array];

    for (NSString *string in arr) {
        // 判断元素是否为省
        if (![string hasPrefix:@" "]) {
            // 创建省字典
            NSMutableDictionary *provincesDic = [NSMutableDictionary dictionary];
            [provincesDic setObject:string forKey:@"provinceName"];
            // 市数组 存储每个市字典
            NSMutableArray *citiesArr = [NSMutableArray array];
            // 市数组添加到省字典
            [provincesDic setObject:citiesArr forKey:@"cities"];
            // 省数组添加省字典对象
            [provincesArr addObject:provincesDic];
            
            // 取到所有的市
        } else if ([string hasPrefix:@"  "] && ![string hasPrefix:@"    "]) {
            // 创建市字典
            NSMutableDictionary *citiesDic = [NSMutableDictionary dictionary];
            [citiesDic setObject:string forKey:@"cityName"];
            // 区数组
            NSMutableArray *areasArr = [NSMutableArray array];
            // 把区数组添加到市字典中
            [citiesDic setObject:areasArr forKey:@"areas"];
            // 找到该市所在的省字典
            NSMutableDictionary *proDic = [provincesArr lastObject];
            // 市数组添加市字典对象
            [[proDic objectForKey:@"cities"] addObject:citiesDic];
        } else  {
            // 创建区字典
            NSMutableDictionary *areasDic = [NSMutableDictionary dictionary];
            // 区名添加到区字典中
            [areasDic setObject:string forKey:@"areaName"];
            // 通过省数组获取到当前所在省的字典
            NSMutableDictionary *proDic = [provincesArr lastObject];
            // 通过省字典获取对应的市字典
            NSMutableDictionary *citiesDic = [[proDic objectForKey:@"cities"] lastObject];
            // 找到区数组
            NSMutableArray *areasArr = [citiesDic objectForKey:@"areas"];
            // 区字典添加到区数组
            [areasArr addObject:areasDic];
        }
    }
        // 2.省数组 ProModel类
        NSMutableArray *allProvinceArr = [NSMutableArray array];
        for (NSString *area in arr) {
            if (![area hasPrefix:@" "]) {
                ProModel *proModel = [ProModel areaModelWithAreaName:area];
                [allProvinceArr addObject:proModel];
            } else if ([area hasPrefix:@"  "] && ![area hasPrefix:@"    "]) {
                ProModel *pro = [allProvinceArr lastObject];
                CityModel *city = [CityModel areaModelWithAreaName:area];
                [pro.arr addObject:city];
            } else {
                CityModel *city = [[[allProvinceArr lastObject] arr] lastObject];
                AreaModel *area1 = [AreaModel areaModelWithAreaName:area];
                [city.arr addObject:area1];
            }
        }
    }
#endif
    
#pragma mark - 第五题
    /*处理Location1.txt 将省市区进行分割处理, 省里包含市, 市里包含区
     1. 先用数组字典表示
     2. 将数组字典的处理方式转换成数组模型(Class)
     */
    NSString *path1 = @"/Users/dllo/DLS160710/Objective-C/HomeWork/OC05_Homework/OC05_Homework/Location1.txt";
    NSString *fileStr1 = [NSString stringWithContentsOfFile:path1 encoding:NSUTF8StringEncoding error:nil];
    NSArray *arrLoc = [fileStr1 componentsSeparatedByString:@","];
    
    NSMutableArray *allCityDicArray = [NSMutableArray array];
    // 根据每个城市中的|处理字符串, 重新生成字典的数组
    for (NSString *str in arrLoc) {
        NSArray *arrayCity = [str componentsSeparatedByString:@"|"];
        // 根据切割生成的数组 生成新的字典
        NSMutableDictionary *cityDic = [NSMutableDictionary dictionaryWithObjectsAndKeys:[arrayCity firstObject], @"cityName", [arrayCity lastObject], @"cityNumber", nil];
        [allCityDicArray addObject:cityDic];
    }
    // 排序(重要)
    NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"cityNumber" ascending:YES];
    [allCityDicArray sortUsingDescriptors:@[sort]];
    
        NSMutableArray *finalArray = [NSMutableArray array];
        for (NSMutableDictionary *cityDic in allCityDicArray) {
            NSString *cityNumber = cityDic[@"cityNumber"];
            if ([cityNumber hasSuffix:@"0000"]) {
                NSMutableDictionary *proDic = [NSMutableDictionary dictionaryWithDictionary:cityDic];
                [proDic setObject:[NSMutableArray array] forKey:@"cityArray"];
                [finalArray addObject:proDic];
            } else if ([cityNumber hasSuffix:@"00"] && ![cityNumber hasSuffix:@"0000"]) {
                NSMutableDictionary *proDic = [finalArray lastObject];
                NSMutableArray *cityArray = [proDic objectForKey:@"cityArray"];
                NSMutableDictionary *dicCity = [NSMutableDictionary dictionaryWithDictionary:cityDic];
                [dicCity setObject:[NSMutableArray array] forKey:@"zoneArray"];
                [cityArray addObject:dicCity];
            } else {
                NSMutableDictionary *proDic = [finalArray lastObject];
                NSMutableArray *zoneArray = [[[proDic objectForKey:@"cityArray"] lastObject] objectForKey:@"zoneArray"];
                NSMutableDictionary *dicZone = [NSMutableDictionary dictionaryWithDictionary:cityDic];
                [zoneArray addObject:dicZone];
            }
        }
        NSLog(@"%@", finalArray);
    
    // 第二问
    NSMutableArray *allTempCityArray = [NSMutableArray array];
    for (NSString *cityString in arrLoc) {
        NSArray *city = [cityString componentsSeparatedByString:@"|"];
        TempCity *tempCity = [[TempCity alloc] initWithName:[city firstObject] number:[city lastObject]];
        [allTempCityArray addObject:tempCity];
    }
    NSSortDescriptor *sortTempCity = [[NSSortDescriptor alloc] initWithKey:@"cityNumber" ascending:YES];
    [allTempCityArray sortUsingDescriptors:@[sortTempCity]];
    
    NSMutableArray *finalClassArray = [NSMutableArray array];
    for (TempCity *tempCity in allTempCityArray) {
        if ([tempCity.cityNumber hasSuffix:@"0000"]) {
            ProModel *proModel = [ProModel areaModelWithTempCity:tempCity];
            [finalClassArray addObject:proModel];
        } else if ([tempCity.cityNumber hasSuffix:@"00"] && ![tempCity.cityNumber hasSuffix:@"0000"]) {
            ProModel *proModel = [finalClassArray lastObject];
            CityModel *cityModel = [CityModel areaModelWithTempCity:tempCity];
            [proModel.arr addObject:cityModel];
        } else {
            ProModel *proModel = [finalClassArray lastObject];
            CityModel *city = [proModel.arr lastObject];
            AreaModel *area = [AreaModel areaModelWithTempCity:tempCity];
            [city.arr addObject:area];
        }
    }
//    NSLog(@"%@", finalClassArray);
    
    
    return 0;
}
